package com.gezhi.stuserver.stumag.mapper;

import com.gezhi.stuserver.bean.ClassBean;
import com.gezhi.stuserver.bean.StudentBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;

/**
 * 学生持久层接口
 *
 * @author AWG
 */
@Mapper
public interface StudentMapper {

    /**
     * 新增学生
     *
     * @param studentBean 学生对象
     */
    @Insert("insert into t_student (f_student_num, f_student_name, f_gender, f_age, fk_cls_id)values (#{s.studentNum}, #{s.studentName}, #{s.gender}, #{s.age}, #{s.cls.id})")
    void saveStudent(@Param("s") StudentBean studentBean);

    /**
     * 修改学生
     *
     * @param studentBean 学生对象
     */
    void updateStudent(@Param("s") StudentBean studentBean);

    /**
     * 删除学生
     *
     * @param studentBean 学生对象
     */
    @Delete("delete from t_student where pk_id = #{s.id}")
    void deleteStudent(@Param("s") StudentBean studentBean);

    /**
     * 根据学生ID查询学生
     *
     * @param id 学生ID
     * @return 学生对象
     */
    @ResultType(StudentBean.class)
    @Results(id = "studentResult",value = {
            @Result(property = "id",column = "pk_id"),
            @Result(property = "studentNum",column = "f_student_num"),
            @Result(property = "studentName",column = "f_student_name"),
            @Result(property = "gender",column = "f_gender"),
            @Result(property = "age",column = "f_age"),
            @Result(property = "cls",column = "fk_cls_id", one = @One(select = "getClassById", fetchType = FetchType.LAZY))
    })
    @Select("select pk_id, f_student_num, f_student_name, f_gender, f_age, fk_cls_id from t_student where pk_id=#{id}")
    StudentBean getStudentById(@Param("id") Integer id);

    /**
     * 查询所有的学生
     *
     * @return 所有的学生组成的List集合
     */
    @ResultMap(value = "studentResult")
    @Select("select pk_id, f_student_num, f_student_name, f_gender, f_age, fk_cls_id from t_student")
    List<StudentBean> findAllStudent();

    /**
     * 查询所有的班级
     * @param id 班级ID
     * @return 所有的班级组成的List集合
     */
    @Select("select pk_id as id,f_cls_name as clsName from t_cls where pk_id=#{id}")
    ClassBean getClassById(@Param("id") Integer id);

}
